package LeetCode.month03.day78;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author 16778
 * @date 2025/10/18 8:07
 * @description
 */
public class No3397 {
    static void main() {
        maxDistinctElements(new int[]{1,2,2,3,3,4},2);
    }
    public static int maxDistinctElements(int[] nums, int k) {
        Arrays.sort(nums);
        int n = nums.length;
        int min = nums[0]-k;
        Set<Integer> set = new HashSet<>();
        set.add(min);
        for (int i = 1; i < n; i++) {
            if(nums[i]-k>min){
                set.add(nums[i]-k);
                min = nums[i]-k;
            }else if(nums[i]+k<=min){
                continue;
            }else{
                set.add(++min);
            }
        }
        return set.size();
    }

    public int maxDistinctElements2(int[] nums,int k){
        Arrays.sort(nums);
        int res = 0;
        int pre = Integer.MIN_VALUE;
        for (int num : nums) {
            int x = Math.min(num+k,Math.max(pre+1,num-k));
            if(x>pre){
                res++;
                pre = x;
            }
        }
        return res;
    }
}
